﻿
//创建时间: 2010-10-19
//创建作者: 李晓忠
//功能说明: CMS系统 信息基表 
//最后修改: 2010-10-19
//最后修改: 2010-10-20 李晓忠 添加保存模型信息。
//最后修改: 2010-10-21 李晓忠 GetDataSet。修改SaveNewDoc
//最后修改: 2010-11-2 李晓忠 添加置顶操作\精华操作\排序操作
//最后修改: 2010-11-17 李晓忠 获取信息标题值
//最后修改: 2010-11-22 李晓忠 添加更新计划发布时间
//最后修改: 2010-12-8 李晓忠 添加虚链接,获取信息发布点路径
using System;
using System.Collections.Generic;
using System.Text;


using TRPOP.CMS.Model;
using TRPOP.CMS.DALFactory;
using System.Data;
using TRPOP.Common;
using TRPOP.CMS.Plugin;
using System.Text.RegularExpressions;
using TRPOP.Config;
using TRPOP.Cache;

namespace TRPOP.CMS.Business
{
    public class ContentManage
    {

        private IContentDAL Idal;
        public ContentManage()
        {
            Idal = DataAccess.CreateContentDAL();
        }

        #region 结点数量限制的辅助方法
        /// <summary>
        /// 是否溢出
        /// </summary>
        /// <returns></returns>
        public bool IsOverFlow()
        {
            if (TRPOP.CMS.Plugin.VersionLimit.ContentLimit == -1)
                return false;
            return GetDataSet(SystemInfoHelper.getSystemInfo().DBprefixTableName + "_" + "Content_Content", "Contentid", "", "").Tables[0].Rows.Count >= TRPOP.CMS.Plugin.VersionLimit.ContentLimit;
        }
        #endregion

        #region 当编辑时删除所有已之相关的缓存
        /// <summary>
        /// 当编辑时删除所有已之相关的缓存
        /// </summary>
        /// <param name="oEnumSubmitResult"></param>
        /// <returns></returns>
        public EnumSubmitResult RemoveCacheWhenEdit(EnumSubmitResult oEnumSubmitResult,ContentInfo info)
        {
            ICache oICache = ICacheAccess.GetInstance();
            if (oEnumSubmitResult == EnumSubmitResult.Success)
            {
                oICache.RemoveByRegexp(string.Format(CacheKeys.Template_ContentInfo_IndexID, info.ContentId));
                oICache.RemoveByRegexp(CacheKeys.Template_List_AllKeys);
            }
            return oEnumSubmitResult;
        }
        #endregion

        #region 保存 Save
        ///	<summary>
        ///	保存
        ///	</summary>
        ///	<param name="ContentInfo"></param>
        ///	<returns>EnumSubmitResult</returns>
        public EnumSubmitResult Save(ContentInfo info)
        {
            return RemoveCacheWhenEdit(Idal.Save(info), info);
        }
        ///	<summary>
        ///	保存
        ///	</summary>
        ///	<param name="ContentInfo"></param>
        ///	<returns>EnumSubmitResult</returns>
        public EnumSubmitResult Save(ContentInfo info,bool blnIsRemoveCache)
        {
            if (blnIsRemoveCache)
                return RemoveCacheWhenEdit(Idal.Save(info), info);
            return Idal.Save(info);
        }

        #endregion

        #region 获取实体 GetInfoById
        ///	<summary>
        ///	获取实体
        ///	</summary>
        ///	<param name="contentId"></param>
        ///	<returns>ContentInfo</returns>
        public ContentInfo GetInfoById(int id)
        {
            return Idal.GetInfoById(id);
        }
        #endregion

        #region 删除 DeleteInfoById
        ///	<summary>
        ///	删除
        ///	</summary>
        ///	<param name="id"></param>
        ///	<returns>EnumSubmitResult</returns>
        public EnumSubmitResult DeleteInfoById(int id)
        {
            return DeleteInfoByIds(id.ToString());
        }
        #endregion

        #region 删除 DeleteInfoByIds
        ///	<summary>
        ///	删除
        ///	</summary>
        ///	<param name="ids"></param>
        ///	<returns>EnumSubmitResult</returns>
        public EnumSubmitResult DeleteInfoByIds(string ids)
        {
            Idal.DeleteInfoByIds(ids);
            RemoveCacheWhenEdit(EnumSubmitResult.Success,new ContentInfo());
            return EnumSubmitResult.Success;
        }
        #endregion

        #region 自定义列表（无分页） GetListByCondition
        ///	<summary>
        ///	自定义列表（无分页）
        ///	</summary>
        ///	<param name="topn">前N条，为0时表示所有符合条件的</param>
        ///	<param name="condition">自定义条件，要以And开头</param>
        ///	<param name="orderby">排序，不需要Order by </param>
        ///	<returns>List<ContentInfo></returns>
        public List<ContentInfo> GetListByCondition(int topn, string condition, string orderby)
        {
            return Idal.GetListByCondition(topn, condition, orderby);
        }
        #endregion

        #region 自定义列表（分页） GetListByPager
        ///	<summary>
        ///	自定义列表（分页）
        ///	</summary>
        ///	<returns>List<ContentInfo></returns>
        public List<ContentInfo> GetListByPager(int pageNumber, int pageSize, string condition, string orderby, out int recordCount)
        {
            return Idal.GetListByPager(pageNumber, pageSize, condition, orderby, out  recordCount);
        }
        #endregion

        #region 保存信息 SaveNewDoc
        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="info">基本信息</param>
        /// <param name="model">模型</param>
        /// <param name="modelfieldlist">模型字段</param>
        /// <returns></returns>
        public EnumSubmitResult SaveNewDoc(ContentInfo info, List<ModelFieldInfoWithTableName> modelfieldlist)
        {
            return  Idal.SaveNewDoc(info,modelfieldlist);;
        }
        #endregion

        #region 获取信息 GetDataSet

        /// <summary>
        /// 获取信息 GetDataSet
        /// </summary>
        /// <param name="strTable">表名</param>
        /// <param name="strFields">字段</param>
        /// <param name="strCondition">条件</param>
        /// <param name="strOrderBy">排序</param>
        /// <returns></returns>
        public DataSet GetDataSet(string strTable, string strFields, string strCondition, string strOrderBy)
        {
            return Idal.GetDataSet(strTable, strFields, strCondition, strOrderBy);
        }
        #endregion

        #region 删除扩展信息  DeleteInfoByExtendId
        /// <summary>
        /// 删除扩展信息
        /// </summary>
        /// <param name="ExtendId"></param>
        /// <param name="ExtendTableName"></param>
        /// <returns></returns>
        public EnumSubmitResult DeleteInfoByExtendId(int ExtendId, string ExtendTableName)
        {
            return Idal.DeleteInfoByExtendId(ExtendId, ExtendTableName);
        }
        #endregion

        #region 置顶操作 SetTopWeightByIds
       /// <summary>
        /// 置顶操作
       /// </summary>
       /// <param name="NodeId"></param>
       /// <param name="ExtendIds"></param>
       /// <param name="TopWeight"></param>
       /// <returns></returns>
        public EnumSubmitResult SetTopWeightByIds( string Ids, int TopWeight)
        {
            return Idal.SetTopWeightByIds(Ids, TopWeight);
        }
        #endregion

        #region 精华操作 SetEliteWeightByIds
       /// <summary>
        /// 精华操作
       /// </summary>
       /// <param name="NodeId"></param>
       /// <param name="ExtendIds"></param>
       /// <param name="EliteWeight"></param>
       /// <returns></returns>
        public EnumSubmitResult SetEliteWeightByIds(string Ids, int EliteWeight)
        {
            return Idal.SetEliteWeightByIds(Ids,EliteWeight);
        }
        #endregion

        #region 排序操作 SetDisplayOrderByIds
        /// <summary>
        /// 排序操作
        /// </summary>
        /// <param name="NodeId"></param>
        /// <param name="ExtendIds"></param>
        /// <param name="DisplayOrder"></param>
        /// <returns></returns>
        public EnumSubmitResult SetDisplayOrderByIds(string Ids, int DisplayOrder)
        {
            return Idal.SetDisplayOrderByIds(Ids, DisplayOrder);
        }
        #endregion

        #region 恢复逻辑删除
        /// <summary>
        ///  恢复逻辑删除 PubStatus=0
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public EnumSubmitResult RestoreDeleteInfoByIds(string ids)
        {
            return Idal.RestoreDeleteInfoByIds(ids);
        }
        #endregion

        #region 永久删除
        /// <summary>
        /// 永久删除, 后面需要添加删除相关评论等信息。
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public  EnumSubmitResult DestroyDeleteInfoByIds(string ids)
        {
            return Idal.DestroyDeleteInfoByIds(ids);
        }
        #endregion

        #region  清空回收站 EmptyRecycleByNodeId
        /// <summary>
        /// 清空回收站 EmptyRecycleByNodeId
        /// </summary>
        /// <param name="NodeId"></param>     
        /// <param name="isDelAttr">删除上传附件</param>
        /// <param name="isDelImg">删除上传图片</param>
        /// <returns></returns>
        public EnumSubmitResult EmptyRecycleByNodeId(int NodeId, bool isDelAttr,bool isDelImg)
        {
            EnumSubmitResult enumResult = EnumSubmitResult.Success;
            string strCondition = "   AND  PubStatus=-1  AND NodeId=" + NodeId;
            string strOrderBy = "";
            List<ContentInfo> m_ContentInfoList = GetListByCondition(0, strCondition, strOrderBy);
            NodeInfo m_NodeInfo = (new NodeManage()).GetInfoById(NodeId);
            if (m_NodeInfo != null)
            {
                foreach (ContentInfo info in m_ContentInfoList)
                {
                    enumResult = DestroyDeleteInfoById(info, false, isDelAttr,isDelImg);
                }
            }
            return enumResult;
        }
        #endregion

        #region 文章转移操作 CutDocToNewNode
        /// <summary>
        /// 文章转移操作
        /// </summary>
        /// <param name="Ids"></param>
        /// <param name="NodeId"></param>
        /// <returns></returns>
        public EnumSubmitResult CutDocToNewNode(string Ids, int NodeId)
        {
            return Idal.CutDocToNewNode(Ids, NodeId);
        }
        #endregion

        #region  文章复制操作CopyDocToNewNode
        /// <summary>
        /// 文章复制操作CopyDocToNewNode
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="NodeId"></param>
        /// <param name="AdminId"></param>
        /// <param name="AdminTime"></param>
        /// <returns></returns>
        public EnumSubmitResult CopyDocToNewNode(string ids, int NodeId, int AdminId, DateTime AdminTime)
        {
            return Idal.CopyDocToNewNode(ids, NodeId, AdminId, AdminTime);
        }
        #endregion

        #region  永久删除 DestroyDeleteInfoByNodeId
        /// <summary>
        /// 永久删除, 后面需要添加删除相关评论等信息。
        /// </summary>
        /// <param name="nodeid">结点Id</param>
        /// <returns></returns>
        public EnumSubmitResult DestroyDeleteInfoByNodeId(int nodeid)
        {
            //需要添加删除文件
            return Idal.DestroyDeleteInfoByNodeId(nodeid); ;
        }
        #endregion

        #region 获取信息表1条数据
        /// <summary>
        /// 获取信息表1条数据
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="ExtendId"></param>
        /// <param name="m_ModelFieldInfoList"></param>
        /// <returns></returns>
        public List<ModelFieldInfoWithTableName> GetContentData(string tableName, int ExtendId, List<ModelFieldInfoWithTableName> m_ModelFieldInfoList)
        {
            return Idal.GetContentData(tableName, ExtendId, m_ModelFieldInfoList);
        }
        #endregion

        #region 获取信息标题值
        /// <summary>
        /// 获取信息标题值
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="nodeid">结点id</param>
        /// <param name="ContentId">基表信息id</param>
        /// <returns></returns>
        public string GetContentTitle(string tableName,int nodeid,  int ContentId)
        {
            string str = "";
            ModelFieldInfo titleField=(new ModelFieldManage()).GetTableTitleFieldByNodeId(nodeid);
           if(titleField!=null)
           {
              return  Idal.GetFieldValue(tableName, titleField.FieldName, ContentId).ToString();
            }
            return str;
        }
        #endregion

        #region 更新计划发布时间
        /// <summary>
        /// 更新计划发布时间
        /// </summary>
        /// <param name="Ids"></param>
        /// <param name="PubTime"></param>
        /// <returns></returns>
        public EnumSubmitResult SetPubTimeByIds(string Ids, DateTime PubTime)
        {
            return Idal.SetPubTimeByIds(Ids, PubTime);
        }
        #endregion

        #region 获取结点下所有的信息数
        /// <summary>
        /// 获取结点下所有的信息数
        /// </summary>
        /// <param name="strNodeId"></param>
        /// <param name="intPubstatus"></param>
        /// <returns></returns>
        public int  GetContentCount(string strNodeId, string strPubstatus)
        {
            return Idal.GetContentCount(strNodeId, strPubstatus);
        }
        #endregion

        #region 获取关键字相关信息的Id
        /// <summary>
        /// 获取关键字相关信息的Id
        /// </summary>
        /// <param name="SearchKey">搜索关键字</param>
        /// <param name="NodeId">结点Id</param>
        /// <returns></returns>
        public string GetExtendIdBySearchKey(string SearchKey, int NodeId)
        {
            return Idal.GetExtendIdBySearchKey(SearchKey, NodeId);
        }

        /// <summary>
        /// 查询关键字 拼接查询的SQL语句
        /// </summary>
        /// <param name="SearchKey">关键字</param>
        /// <param name="NodeId">结点id</param>
        /// <returns></returns>
        public string GetExtendIdBySearchKeySQL(string SearchKey, int NodeId)
        {
            return Idal.GetExtendIdBySearchKeySQL(SearchKey, NodeId);
        }

        #endregion

        #region 添加虚链接
       /// <summary>
        /// 添加虚链接
       /// </summary>
       /// <param name="oldNodeId">旧结点</param>
       /// <param name="newNodeId">新结点</param>
       /// <param name="ContentIds">信息Id</param>
       /// <param name="result">处理结果</param>
       /// <param name="msg">结果说明</param>
       /// <returns></returns>
        public List<ContentInfo> CreateVirtualLink(int oldNodeId, int newNodeId, string ContentIds,out bool result,out string msg)
        {
            List<ContentInfo> m_ContentInfoList=new List<ContentInfo>();
            NodeInfo m_oldNodeInfo = new NodeInfo();
            NodeInfo m_newNodeInfo = new NodeInfo();
            NodeManage m_NodeManage = new NodeManage();
            result = true;
            msg = "";

            #region 结点模型判断
            m_oldNodeInfo = m_NodeManage.GetInfoById(oldNodeId);
            m_newNodeInfo = m_NodeManage.GetInfoById(newNodeId);

            if (m_oldNodeInfo == null || m_newNodeInfo == null)
            {
                result = false;
                msg = "操作失败,原因信息所属结点或要建链接的结点不存在了";
                
            }
            else
            {
                if (m_oldNodeInfo.ModelId != m_newNodeInfo.ModelId)
                {
                    result = false;
                    msg = "操作失败，原因选择的结点与原来的模型不一致";
                }
            }
            #endregion

            if (result)
            {
                #region 开始操作
                if (ContentIds != "")
                {
                    m_ContentInfoList = GetListByCondition(0, " AND ContentId IN (" + ContentIds + ")  AND [Type]=" + (int)LinkType.EnumElement.RealLink, "");
                    foreach (ContentInfo info in m_ContentInfoList)
                    {
                        info.ContentId = 0;
                        info.NodeId = newNodeId;
                        info.Type = (int)LinkType.EnumElement.VirtualLink;
                        info.Template = "";
                        info.PubStatus = 0;
                        info.TopWeight = 0;
                        info.EliteWeight = 0;
                        info.DisplayOrder = 0;
                        info.PubTime = DateTime.Now;
                        Save(info);
                        //info.Url = CreateURL(m_newNodeInfo, info);//生成URL                              
                    }
                    result = true;
                    msg = "操作成功";
                }
                else
                {
                    result = false;
                    msg = "操作失败,原因信息Id为0";
                }
                #endregion
            }


            return m_ContentInfoList;
        }
        #endregion

        #region 信息 URL生成，并保存
        /// <summary>
        /// 生成URL，并保存
        /// </summary>
        /// <param name="nodeInfo"></param>
        /// <param name="contentInfo"></param>
        /// <returns></returns>
        public string CreateURL(NodeInfo nodeInfo, ContentInfo contentInfo)
        {
            string strURL = string.Empty;
            NodeManage m_NodeManage = new NodeManage();
            string strPageName = string.Empty;
            if (nodeInfo.ContentPubTypeId == (int)ContentPubType.EnumElement.DynamicPub)
            {
                #region 动态发布
                if (!(contentInfo.ExtraUrl).IsEmpty())//外链接
                {
                    strURL = contentInfo.ExtraUrl;
                    contentInfo.Url = contentInfo.ExtraUrl;
                    contentInfo.Type = (int)LinkType.EnumElement.OutLink;
                }
                else if (contentInfo.Type == (int)LinkType.EnumElement.IndexLink)//索引链接
                {

                }
                else
                {
                    if (contentInfo.Type == (int)LinkType.EnumElement.OutLink)
                        contentInfo.Type = (int)LinkType.EnumElement.RealLink;//如果是外链接，则修改为实链接
                    strPageName = m_NodeManage.PageName(nodeInfo.ContentPortalURL, contentInfo.ExtendId, contentInfo.NodeId, contentInfo.ContentId, 1, contentInfo.PubTime);
                    contentInfo.Url = strPageName;
                    strURL = strPageName;
                }
                #endregion
            }
            else if (nodeInfo.ContentPubTypeId == (int)ContentPubType.EnumElement.StaticPub)
            {
                #region 静态发布
                if (!contentInfo.ExtraUrl.IsEmpty())//外链接
                {
                    strURL = contentInfo.ExtraUrl;
                    contentInfo.Url = contentInfo.ExtraUrl;
                    contentInfo.Type = (int)LinkType.EnumElement.OutLink;
                }
                else if (contentInfo.Type == (int)LinkType.EnumElement.IndexLink)//索引链接
                {

                }
                else//实链接
                {
                    if (contentInfo.Type == (int)LinkType.EnumElement.OutLink)
                        contentInfo.Type = (int)LinkType.EnumElement.RealLink;//如果是外链接，则修改为实链接
                    if (contentInfo.HtmlName.IsEmpty())//文件名
                    {
                        strPageName = m_NodeManage.PageName(nodeInfo.HtmlNameReg, contentInfo.ExtendId, contentInfo.NodeId, contentInfo.ContentId, 0, contentInfo.PubTime);
                    }
                    else
                    {
                        strPageName = contentInfo.HtmlName;
                    }
                    if (!contentInfo.SelfURL.IsEmpty())//自定义发布URL
                    {
                        strURL = (new PSNManage()).GetPSNURLByPSNUrl(contentInfo.SelfURL);
                    }
                    else if (!nodeInfo.HtmlUrl.IsEmpty())//根据结点设置URL
                    {
                        strURL = (new PSNManage()).GetPSNURLByPSNUrl(nodeInfo.HtmlUrl);
                    }
                    if (nodeInfo.HtmlRootReg != "")
                    {
                        strURL = strURL + "/" + m_NodeManage.RootReg(nodeInfo.HtmlRootReg, contentInfo.PubTime);
                    }
                    contentInfo.Url = strURL + "/" + strPageName;
                    strURL = contentInfo.Url;
                }
                #endregion
            }
            else if (nodeInfo.ContentPubTypeId == (int)ContentPubType.EnumElement.NotOutPub)
            {
                #region 不发布
                if (!contentInfo.ExtraUrl.IsEmpty())//外连接
                {
                    strURL = contentInfo.ExtraUrl;
                    contentInfo.Url = contentInfo.ExtraUrl;
                    contentInfo.Type = (int)LinkType.EnumElement.OutLink;
                }
                else if (contentInfo.Type == (int)LinkType.EnumElement.IndexLink)//索引链接
                {

                }
                else
                {
                    if (contentInfo.Type == (int)LinkType.EnumElement.OutLink)
                        contentInfo.Type = (int)LinkType.EnumElement.RealLink;//如果是外链接，则修改为实链接
                    contentInfo.Url = "";
                }
                #endregion
            }
            Save(contentInfo);
            return strURL;
        }
        #endregion

        #region 获取实链接的虚链接信息
        /// <summary>
        /// 获取实链接的虚链接信息
        /// </summary>
        /// <param name="ContentIds"></param>
        /// <returns></returns>
        public List<ContentInfo> GetVirtualLink(string ContentIds)
        {
            return Idal.GetVirtualLink(ContentIds) ;
        }
        #endregion

        #region 获取信息发布点路径


        #region 
        /// <summary>
        /// 获取信息发布点路径
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public string GetContentInfoPSNPath(ContentInfo info)
        {
            string strpath = "";
            NodeInfo m_NodeInfo = new NodeInfo();
            NodeManage m_NodeManage = new NodeManage();
            PSNManage m_PSNManage = new PSNManage();
            PSNInfo m_PSNInfo=new PSNInfo();
            m_NodeInfo = m_NodeManage.GetInfoById(info.NodeId);
            string strPsnURL = "";
            string strInfoURL = "";
            string strBasePath = "";
            if (m_NodeInfo != null && m_NodeInfo.ContentPubTypeId == (int)ContentPubType.EnumElement.StaticPub)
            {
                string strPageName = m_NodeManage.PageName(m_NodeInfo.HtmlNameReg, info.ExtendId, info.NodeId, info.ContentId, 1, info.PubTime);

                if (info.ExtraUrl != "")
                {
                    //自定义外链接没有处理
                }
                else if (info.SelfPSN != "")
                {
                     strpath= m_PSNManage.GetPSNPathByPSN(info.SelfPSN);
                       strPsnURL = m_PSNManage.GetPSNURLByPSNUrl(info.SelfURL);
                       strInfoURL = info.Url;
                       if (strInfoURL.IndexOf(strPsnURL) == -1)
                       {

                       }
                       else
                       {
                           strBasePath = strInfoURL.Replace(strPsnURL, "");
                       }
                  
                }
                else if (m_NodeInfo.HtmlPSN != "")
                {
                       strpath = m_PSNManage.GetPSNPathByPSN(m_NodeInfo.HtmlPSN);
                        strPsnURL = m_PSNManage.GetPSNURLByPSNUrl(m_NodeInfo.HtmlUrl);
                        strInfoURL = info.Url;
                        if (strInfoURL.IndexOf(strPsnURL) == -1)
                        {

                        }
                        else
                        {
                            strBasePath = strInfoURL.Replace(strPsnURL, "");
                        }
                }
                else if (SYSConfig.SystemVar_DefaultContentPSN != "")
                {
                    strpath = m_PSNManage.GetPSNPathByPSN(SYSConfig.SystemVar_DefaultContentPSN);
                    strPsnURL = m_PSNManage.GetPSNURLByPSNUrl(SYSConfig.SystemVar_DefaultContentPSNURL);
                    strInfoURL = info.Url;
                    if (strInfoURL.IndexOf(strPsnURL) == -1)
                    {

                    }
                    else
                    {
                        strBasePath = strInfoURL.Replace(strPsnURL, "");
                    }
                }


            }
            return (strpath + "/" + strBasePath).Replace("//","/");
        }

      /// <summary>
        /// 获取信息发布点路径
      /// </summary>
      /// <param name="id"></param>
      /// <returns></returns>
        public string GetContentInfoPSNPath(int  id)
        {
            string strpath = "";
            ContentInfo info = GetInfoById(id);
            if (info != null)
            {
                NodeInfo m_NodeInfo = new NodeInfo();
                NodeManage m_NodeManage = new NodeManage();
                PSNManage m_PSNManage = new PSNManage();
                PSNInfo m_PSNInfo = new PSNInfo();
                m_NodeInfo = m_NodeManage.GetInfoById(info.NodeId);
                string strPsnURL = "";
                string strInfoURL = "";
                string strBasePath = "";
                if (m_NodeInfo != null && m_NodeInfo.ContentPubTypeId == (int)ContentPubType.EnumElement.StaticPub)
                {
                    if (info.ExtraUrl != "")
                    {
                        //自定义外链接没有处理
                    }
                    else if (info.SelfPSN != "")
                    {
                        strpath = m_PSNManage.GetPSNPathByPSN(info.SelfPSN);
                        strPsnURL = m_PSNManage.GetPSNURLByPSNUrl(info.SelfURL);
                        strInfoURL = info.Url;
                        strBasePath = strInfoURL.Replace(strPsnURL, "");

                    }
                    else if (m_NodeInfo.HtmlPSN != "")
                    {
                        strpath = m_PSNManage.GetPSNPathByPSN(m_NodeInfo.HtmlPSN);
                        strPsnURL = m_PSNManage.GetPSNURLByPSNUrl(m_NodeInfo.HtmlUrl);
                        strInfoURL = info.Url;
                        strBasePath = strInfoURL.Replace(strPsnURL, "");
                    }
                    else if (SYSConfig.SystemVar_DefaultContentPSN != "")
                    {
                        strpath = m_PSNManage.GetPSNPathByPSN(SYSConfig.SystemVar_DefaultContentPSN);
                        strPsnURL = m_PSNManage.GetPSNURLByPSNUrl(SYSConfig.SystemVar_DefaultContentPSNURL);
                        strInfoURL = info.Url;
                        strBasePath = strInfoURL.Replace(strPsnURL, "");
                    }
                }
            }
            return strpath;
        }
        #endregion

        #endregion

        #region 永久删除结点下信息
       /// <summary>
        ///  永久删除结点下信息
       /// </summary>
       /// <param name="NodeId"></param>
       /// <param name="isDelFile">是否删除发布页面</param>
       /// <param name="isDelAttr">删除附件</param>
       /// <param name="isDelImg">删除上传图片</param>
       /// <returns></returns>
        public EnumSubmitResult DeleteInfoByNodeId(int NodeId, bool isDelFile, bool isDelAttr,bool isDelImg)
        {
            EnumSubmitResult enumResult = EnumSubmitResult.Success;
            string strCondition = " AND NodeId="+NodeId;
            string strOrderBy = "";
            List<ContentInfo> m_ContentInfoList = GetListByCondition(0,strCondition,strOrderBy);
            NodeInfo m_NodeInfo = (new NodeManage()).GetInfoById(NodeId);
            if (m_NodeInfo != null)
            {
                foreach (ContentInfo info in m_ContentInfoList)
                {
                   enumResult= DestroyDeleteInfoById(info, isDelFile,false,false);
                }

                #region 删除附件
                if (isDelAttr || isDelImg)
                {
                    ContentUpFileManage m_ContentUpFileManage = new ContentUpFileManage();
                    m_ContentUpFileManage.DeleteInfoByNodeId(m_NodeInfo.NodeId,isDelAttr,isDelImg);
                }
                #endregion
            }
            return enumResult;
        }
        #endregion

        #region 永久删除信息,删除相关的虚连接，索引连接
       /// <summary>
       /// 永久删除信息,删除相关的虚连接，索引连接
       /// </summary>
       /// <param name="id"></param>
       /// <param name="isDelFile">删除页面</param>
       /// <param name="isDelAttr">删除附件 </param>
       /// <param name="isDelImg">删除图片</param>
       /// <returns></returns>
        public EnumSubmitResult DestroyDeleteInfoById(int id, bool isDelFile, bool isDelAttr,bool isDelImg)
        {
            EnumSubmitResult enumResult = EnumSubmitResult.Success;
            ContentInfo info = GetInfoById(id);
            if (info != null)
            {
                if (info.Type == (int)LinkType.EnumElement.VirtualLink || info.Type == (int)LinkType.EnumElement.RealLink)
                {
                    #region 删除发布页面
                    if (isDelFile)
                    {
                        if (info.PubStatus == (int)PubStatus.EnumElement.Publish)
                        {
                            string filePath = GetContentInfoPSNPath(info);
                            FileUtil.DeleteFile(filePath, true, "_");
                        }
                    }

                    #endregion
                }

                #region 删除附件
                if (isDelAttr || isDelImg)
                {
                    ContentUpFileManage m_ContentUpFileManage = new ContentUpFileManage();
                    m_ContentUpFileManage.DeleteInfoByContentId(info.ContentId.ToString(),isDelAttr,isDelImg);
                }
                #endregion

                if (info.Type == (int)LinkType.EnumElement.RealLink || info.Type == (int)LinkType.EnumElement.OutLink)
                {
                    #region 获取虚链接，并删除发布页
                    if (isDelFile)
                    {
                        List<ContentInfo> m_ContentInfoList = GetVirtualLink(info.ContentId.ToString());

                        foreach (ContentInfo m_Info in m_ContentInfoList)
                        {
                            if (m_Info.Type == (int)LinkType.EnumElement.VirtualLink)
                            {
                                string filePath = GetContentInfoPSNPath(m_Info);
                                FileUtil.DeleteFile(filePath, true, "_");
                            }
                        }
                    }
                    #endregion

                    //删除数据库信息
                 enumResult=  DeleteInfoByExtendId(info.ExtendId, info.ExtendTableName); 
                  
                }
                else if (info.Type == (int)LinkType.EnumElement.VirtualLink || info.Type == (int)LinkType.EnumElement.IndexLink)
                {
                    enumResult=DeleteContendInfoByContendId(info.ContentId);
                }
                

              


            }
            return enumResult;
        }
        
        /// <summary>
        /// 永久删除信息,删除相关的虚连接，索引连接
        /// </summary>
        /// <param name="info"></param>
        /// <param name="isDelFile">删除发布的静态页</param>
        /// <param name="isDelAttr">删除上传附件</param>
        /// <param name="isDelImg">删除上传的图片</param>
        /// <returns></returns>
        public EnumSubmitResult DestroyDeleteInfoById(ContentInfo info, bool isDelFile,bool isDelAttr,bool isDelImg)
        {
            EnumSubmitResult enumResult = EnumSubmitResult.Success;
            
            if (info != null)
            {
                if (info.Type == (int)LinkType.EnumElement.VirtualLink || info.Type == (int)LinkType.EnumElement.RealLink)
                {
                    #region 删除发布页面
                    if (isDelFile)
                    {
                        if (info.PubStatus == (int)PubStatus.EnumElement.Publish)
                        {
                            string filePath = GetContentInfoPSNPath(info);
                            FileUtil.DeleteFile(filePath, true, "_");
                        }
                    }

                    #endregion


                }

                #region 删除附件
                if (isDelAttr)
                {
                    ContentUpFileManage m_ContentUpFileManage = new ContentUpFileManage();
                    m_ContentUpFileManage.DeleteInfoByContentId(info.ContentId);
                }
                #endregion

                if (info.Type == (int)LinkType.EnumElement.RealLink || info.Type == (int)LinkType.EnumElement.OutLink)
                {
                    #region 获取虚链接，并删除发布页
                    if (isDelFile)
                    {
                        List<ContentInfo> m_ContentInfoList = GetVirtualLink(info.ContentId.ToString());

                        foreach (ContentInfo m_Info in m_ContentInfoList)
                        {
                            if (m_Info.Type == (int)LinkType.EnumElement.VirtualLink)
                            {
                                string filePath = GetContentInfoPSNPath(m_Info);
                                FileUtil.DeleteFile(filePath, true, "_");
                            }
                        }
                    }
                    #endregion

                    //删除数据库信息
                    enumResult = DeleteInfoByExtendId(info.ExtendId, info.ExtendTableName);

                }
                else if (info.Type == (int)LinkType.EnumElement.VirtualLink || info.Type == (int)LinkType.EnumElement.IndexLink)
                {
                    enumResult = DeleteContendInfoByContendId(info.ContentId);
                }





            }
            return enumResult;
        }
        #endregion

        #region 删除基表信息(只删除基表信息)
        /// <summary>
        /// 删除基表信息(只删除基表信息)
        /// </summary>
        /// <param name="ContendId"></param>
        /// <returns></returns>
        public EnumSubmitResult DeleteContendInfoByContendId(int ContendId)
        {
            return Idal.DeleteContendInfoByContendId(ContendId);
        }
        #endregion

        #region 处理编辑器里的远程图片
        public string DownContentURLFile(string strHtml, NodeInfo m_NodeInfo)
        {
            string FileRootPath = new PSNManage().GetPathByNodeInfo(m_NodeInfo);
            FileUtil.CheckPathAndCreate(Common.Util.GetMapPath(FileRootPath), true);
            if(FileRootPath.Trim().EndsWith("/")==false)
                FileRootPath = FileRootPath.Trim()+"/";
            if (FileRootPath.IsEmpty())
                return strHtml;
            int index = 0;
            ContentUpFileManage m_ContentUpFileManage = new ContentUpFileManage();
            foreach (Match oMatch in TRPOP.Common.CommonRegex.MatchHttpDownImgFile(strHtml))
            {
                if (oMatch.Success == false)
                    continue;
                index++;
                string strTemp =  TRPOP.Common.DownURLFile.DownLoadFile(oMatch.Groups["url"].Value,
                                                          FileRootPath,
                                                          DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString()
                                                          );
                if (strTemp != null)
                {
                    strHtml = strHtml.Replace(oMatch.Groups["url"].Value, strTemp);
                    #region 将图片添加到附件库
                    ContentUpFileInfo oContentUpFileInfo = new ContentUpFileInfo();
                    oContentUpFileInfo.AddTime = DateTime.Now;
                    oContentUpFileInfo.AddUserId = 1;
                    oContentUpFileInfo.AttributeID = (int)AttributeType.EnumElement.Pic;
                    oContentUpFileInfo.Category = oMatch.Groups["extname"].Value;
                    oContentUpFileInfo.ContentId = 0;
                    oContentUpFileInfo.NodeID = m_NodeInfo.NodeId;
                    oContentUpFileInfo.FileRoot = strTemp;
                    oContentUpFileInfo.FileSize = 0;
                    m_ContentUpFileManage.Save(oContentUpFileInfo);
                    #endregion
                }
            }
            return strHtml;
        }

        #region 处理编辑器里的远程图片
        public string DownContentURLFile(string strHtml, NodeInfo m_NodeInfo, bool blsIsWater, TRPOP.Common.CMSImageHelper.WaterPicLocation oWaterPicLocationEnum)
        {
            if (m_NodeInfo == null || string.IsNullOrEmpty(m_NodeInfo.WaterPic))
                return DownContentURLFile(strHtml, m_NodeInfo);
            string FileRootPath = new PSNManage().GetPathByNodeInfo(m_NodeInfo);
            FileUtil.CheckPathAndCreate(Common.Util.GetMapPath(FileRootPath), true);
            if (FileRootPath.Trim().EndsWith("/") == false)
                FileRootPath = FileRootPath.Trim() + "/";
            if (FileRootPath.IsEmpty())
                return strHtml;
            int index = 0;
            ContentUpFileManage m_ContentUpFileManage = new ContentUpFileManage();
            foreach (Match oMatch in TRPOP.Common.CommonRegex.MatchHttpDownImgFile(strHtml))
            {
                if (oMatch.Success == false)
                    continue;
                index++;
                string strTemp;
                //2011.07.09 lcheng 修改了远程下载图片，判断是否需要 【开始】
                if (blsIsWater && string.IsNullOrEmpty(m_NodeInfo.WaterPic) == false)
                {
                    strTemp = TRPOP.Common.DownURLFile.DownLoadFile(oMatch.Groups["url"].Value,
                                                              FileRootPath,
                                                              DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString(),
                                                               oWaterPicLocationEnum,
                                                               m_NodeInfo.WaterPic
                                                              );
                }
                else
                {
                    strTemp = TRPOP.Common.DownURLFile.DownLoadFile(oMatch.Groups["url"].Value,
                                                           FileRootPath,
                                                           DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString()
                                                          );
                }
                // 2011.07.09 lcheng 修改了远程下载图片，判断是否需要 【结束】
                if (strTemp != null)
                {

                    #region 将图片添加到附件库
                    ContentUpFileInfo oContentUpFileInfo = new ContentUpFileInfo();
                    oContentUpFileInfo.AddTime = DateTime.Now;
                    oContentUpFileInfo.AddUserId = 1;
                    oContentUpFileInfo.AttributeID = (int)AttributeType.EnumElement.Pic;
                    oContentUpFileInfo.Category = oMatch.Groups["extname"].Value;
                    oContentUpFileInfo.ContentId = 0;
                    oContentUpFileInfo.NodeID = m_NodeInfo.NodeId;
                    oContentUpFileInfo.FileRoot = strTemp;
                    oContentUpFileInfo.FileSize = 0;
                    m_ContentUpFileManage.Save(oContentUpFileInfo);
                    #endregion

                    ImageTools.ResizeImage(TRPOP.Common.RequestHelper.GetMapPath(strTemp), TRPOP.Common.RequestHelper.GetMapPath(strTemp), SYSConfig.AttachmentSetting.UpImageMaxWidth, int.MaxValue, true, 80);
                    strHtml = strHtml.Replace(oMatch.Groups["url"].Value, strTemp);

                }
            }
            return strHtml;
        }
        #endregion

        #endregion

        #region 获取信息列表
        /// <summary>
        /// 获取信息列表（$_title:标题字段）
        /// </summary>
        /// <param name="nodeId"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <param name="orderby"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public DataSet GetContentListByPager(int nodeId, int pageNumber, int pageSize, string condition, string orderby, out int recordCount)
        {
            return Idal.GetContentListByPager(nodeId, pageNumber, pageSize, condition, orderby, out  recordCount);
        }
        #endregion

        #region 分页获取ContentId
        /// <summary>
        /// 分页获取ContentId
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <param name="orderby"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public List<ContentInfo> GetContetnIDListByPager(int pageNumber, int pageSize, string condition, string orderby, out int recordCount)
        {
          return  Idal.GetContetnIDListByPager(pageNumber, pageSize, condition, orderby, out recordCount);
        }
        #endregion

        #region 同步虚结点
        /// <summary>
        /// 同步虚结点
        /// </summary>
        /// <param name="oVirualNode"></param>
        /// <param name="oParentNode"></param>
        /// <param name="intUserID"></param>
        /// <returns></returns>
        public EnumSubmitResult UpdateAllSyncContent(NodeInfo oVirualNode, NodeInfo oParentNode, int intUserID)
        {
            return Idal.UpdateAllSyncContent(oVirualNode, oParentNode, intUserID);
        }
        #endregion
    }
}
